package com.aquarium.config;

import android.R.integer;
import android.util.Log;

public class BTCommand {
	private final static String TAG = "BTCommad";
	public final static int CODE_AC_SW_MASK = 0x40;
	public final static int CODE_AC_SW_01 = 0x4A;
	public final static int CODE_AC_SW_02 = 0x4B;
	public final static int CODE_AC_SW_03 = 0x4C;
	public final static int CODE_AC_SW_04 = 0x4D;
	public final static int CODE_AC_SW_05 = 0x4E;
	public final static int CODE_AC_SW_06 = 0x4F;

	public final static int CODE_SET_PASSWORD = 0x60;
	public final static int CODE_VARIFY_PASSWORD = 0x61;

	public final static int CODE_SET_HP_VALUE = 0x62;
	public final static int CODE_SET_TEMPERATURE_VALUE = 0x63;
	public final static int CODE_SET_OXYGEN_VALUE = 0x64;
	public final static int CODE_SET_CONDUCTIVITY_VALUE = 0x65;
	public final static int CODE_SET_WATER_LEVEL_VALUE = 0x66;

	public final static int CODE_SET_ALIAS = 0x67;

	public final static int CODE_GET_PH_VALUE = 0x68;
	public final static int CODE_GET_TEMPERATURE_VALUE = 0x69;
	public final static int CODE_GET_OXYGEN_VALUE = 0x6A;
	public final static int CODE_GET_CONDUCTIVITY_VALUE = 0x6B;
	public final static int CODE_GET_WATER_LEVEL_VALUE = 0x6C;

	public final static int PH_DATA_LENGTH = 2;
	public final static int TEMPRATURE_DATA_LENGTH = 1;
	public final static int OXYGEN_DATA_LENGTH = 2;
	public final static int CONDUCTIVITY_DATA_LENGTH = 3;
	public final static int WATER_LEVEL_DATA_LENGTH = 2;

	public final static int PH_ALARM_DATA_LENGTH = 4;
	public final static int TEMPRATURE_ALARM_DATA_LENGTH = 2;
	public final static int OXYGEN_ALARM_DATA_LENGTH = 4;
	public final static int CONDUCTIVITY_ALARM_DATA_LENGTH = 6;
	public final static int WATER_LEVEL_ALARM_DATA_LENGTH = 4;

	public final static byte[] AC_SW_O1_ON = { 0x5A, 0x01, CODE_AC_SW_01, 0x01,
			0x00 };
	public final static byte[] AC_SW_O1_OFF = { 0x5A, 0x01, CODE_AC_SW_01,
			0x01, 0x01 };
	public final static byte[] AC_SW_O2_ON = { 0x5A, 0x01, CODE_AC_SW_02, 0x01,
			0x00 };
	public final static byte[] AC_SW_O2_OFF = { 0x5A, 0x01, CODE_AC_SW_02,
			0x01, 0x01 };
	public final static byte[] AC_SW_O3_ON = { 0x5A, 0x01, CODE_AC_SW_03, 0x01,
			0x00 };
	public final static byte[] AC_SW_O3_OFF = { 0x5A, 0x01, CODE_AC_SW_03,
			0x01, 0x01 };
	public final static byte[] AC_SW_O4_ON = { 0x5A, 0x01, CODE_AC_SW_04, 0x01,
			0x00 };
	public final static byte[] AC_SW_O4_OFF = { 0x5A, 0x01, CODE_AC_SW_04,
			0x01, 0x01 };
	public final static byte[] AC_SW_O5_ON = { 0x5A, 0x01, CODE_AC_SW_05, 0x01,
			0x00 };
	public final static byte[] AC_SW_O5_OFF = { 0x5A, 0x01, CODE_AC_SW_05,
			0x01, 0x01 };
	public final static byte[] AC_SW_O6_ON = { 0x5A, 0x01, CODE_AC_SW_06, 0x01,
			0x00 };
	public final static byte[] AC_SW_O6_OFF = { 0x5A, 0x01, CODE_AC_SW_06,
			0x01, 0x01 };
	public final static byte[] SET_PASSWORD_PREFIX = { 0x5A, 0x01,
			CODE_SET_PASSWORD, 0x04 };
	public final static byte[] VARIFY_PASSWORD_PREFIX = { 0x5A, 0x01,
			CODE_VARIFY_PASSWORD, 0x04 };
	public final static byte[] VARIFY_PASSWORD_SUCESS = { 0x5A, 0x01,
			CODE_VARIFY_PASSWORD, 0x01, 0x00 };
	public final static byte[] VARIFY_PASSWORD_FAIL = { 0x5A, 0x01,
			CODE_VARIFY_PASSWORD, 0x01, 0x01 };
	public final static byte[] SET_PH_VALUE_RANGE_PREFIX = { 0x5A, 0x01,
			CODE_SET_HP_VALUE, PH_ALARM_DATA_LENGTH };
	public final static byte[] SET_TEMPERATURE_VALUE_RANGE_PREFIX = { 0x5A,
			0x01, CODE_SET_TEMPERATURE_VALUE, TEMPRATURE_ALARM_DATA_LENGTH };
	public final static byte[] SET_OXYGEN_VALUE_RANGE_PREFIX = { 0x5A, 0x01,
			CODE_SET_OXYGEN_VALUE, OXYGEN_ALARM_DATA_LENGTH };
	public final static byte[] SET_CONDUCTIVITY_VALUE_RANGE_PREFIX = { 0x5A,
			0x01, CODE_SET_CONDUCTIVITY_VALUE, CONDUCTIVITY_ALARM_DATA_LENGTH };
	public final static byte[] SET_WATER_LEVEL_RANGE_PREFIX = { 0x5A, 0x01,
			CODE_SET_WATER_LEVEL_VALUE, WATER_LEVEL_ALARM_DATA_LENGTH };
	public final static byte[] SET_ALIAS_PEFIX = { 0x5A, 0x01, CODE_SET_ALIAS,
			0x0c };

	public static byte[] getSetPasswdCommand(String passwd) {
		byte pswd[] = passwd.getBytes();
		if (pswd.length != SET_PASSWORD_PREFIX[3]) {
			Log.e(TAG, "set password length error!");
			return null;
		}
		byte cmd[] = new byte[SET_PASSWORD_PREFIX.length + pswd.length];
		for (int i = 0; i < SET_PASSWORD_PREFIX.length; i++) {
			cmd[i] = SET_PASSWORD_PREFIX[i];
		}
		for (int j = 0; j < pswd.length; j++) {
			cmd[SET_PASSWORD_PREFIX.length + j] = pswd[j];
		}
		return cmd;
	}

	public static byte[] getVarifyPasswdCommand(String passwd) {
		byte pswd[] = passwd.getBytes();
		if (pswd.length != VARIFY_PASSWORD_PREFIX[3]) {
			Log.e(TAG, "varify password length error!");
			return null;
		}
		byte cmd[] = new byte[VARIFY_PASSWORD_PREFIX.length + pswd.length];
		for (int i = 0; i < VARIFY_PASSWORD_PREFIX.length; i++) {
			cmd[i] = VARIFY_PASSWORD_PREFIX[i];
		}
		for (int j = 0; j < pswd.length; j++) {
			cmd[VARIFY_PASSWORD_PREFIX.length + j] = pswd[j];
		}
		return cmd;
	}

	public static byte[] getSetPhRangeCommand(float phMin, float phMax) {
		byte ph[] = new byte[PH_ALARM_DATA_LENGTH];
		ph[0] = (byte) phMin;
		ph[1] = (byte) (phMin * 10 % 10);
		ph[2] = (byte) phMax;
		ph[3] = (byte) (phMax * 10 % 10);
		byte cmd[] = new byte[SET_PH_VALUE_RANGE_PREFIX.length + ph.length];
		for (int i = 0; i < SET_PH_VALUE_RANGE_PREFIX.length; i++) {
			cmd[i] = SET_PH_VALUE_RANGE_PREFIX[i];
		}
		for (int j = 0; j < ph.length; j++) {
			cmd[SET_PH_VALUE_RANGE_PREFIX.length + j] = ph[j];
		}
		return cmd;
	}

	public static byte[] getSetTemperatureRangeCommand(float tMin, float tMax) {
		byte tmpr[] = new byte[TEMPRATURE_ALARM_DATA_LENGTH];
		tmpr[0] = (byte) tMin;
		tmpr[1] = (byte) tMax;
		byte cmd[] = new byte[SET_TEMPERATURE_VALUE_RANGE_PREFIX.length
				+ tmpr.length];
		for (int i = 0; i < SET_TEMPERATURE_VALUE_RANGE_PREFIX.length; i++) {
			cmd[i] = SET_TEMPERATURE_VALUE_RANGE_PREFIX[i];
		}
		for (int j = 0; j < tmpr.length; j++) {
			cmd[SET_TEMPERATURE_VALUE_RANGE_PREFIX.length + j] = tmpr[j];
		}
		return cmd;
	}

	public static byte[] getSetOxygenRangeCommand(float oxygenMin,
			float oxygenMax) {
		byte oxygen[] = new byte[OXYGEN_ALARM_DATA_LENGTH];
		oxygen[0] = (byte) (oxygenMin / 256);
		oxygen[1] = (byte) (oxygenMin % 256);
		oxygen[2] = (byte) (oxygenMax / 256);
		oxygen[3] = (byte) (oxygenMax % 256);
		byte cmd[] = new byte[SET_OXYGEN_VALUE_RANGE_PREFIX.length
				+ oxygen.length];
		for (int i = 0; i < SET_OXYGEN_VALUE_RANGE_PREFIX.length; i++) {
			cmd[i] = SET_OXYGEN_VALUE_RANGE_PREFIX[i];
		}
		for (int j = 0; j < oxygen.length; j++) {
			cmd[SET_OXYGEN_VALUE_RANGE_PREFIX.length + j] = oxygen[j];
		}
		return cmd;
	}

	public static byte[] getSetConductivityRangeCommand(float cMin, float cMax) {
		byte cnduc[] = new byte[CONDUCTIVITY_ALARM_DATA_LENGTH];
		cnduc[0] = (byte) cMin;
		cnduc[1] = (byte) (cMin * 1000 % 1000 / 256);
		cnduc[2] = (byte) (cMin * 1000 % 1000 % 256);
		cnduc[3] = (byte) cMax;
		cnduc[4] = (byte) (cMax * 1000 % 1000 / 256);
		cnduc[5] = (byte) (cMax * 1000 % 1000 % 256);

		byte cmd[] = new byte[SET_CONDUCTIVITY_VALUE_RANGE_PREFIX.length
				+ cnduc.length];
		for (int i = 0; i < SET_CONDUCTIVITY_VALUE_RANGE_PREFIX.length; i++) {
			cmd[i] = SET_CONDUCTIVITY_VALUE_RANGE_PREFIX[i];
		}
		for (int j = 0; j < cnduc.length; j++) {
			cmd[SET_CONDUCTIVITY_VALUE_RANGE_PREFIX.length + j] = cnduc[j];
		}
		return cmd;
	}

	public static byte[] getSetWaterLevelRangeCommand(float waterMin,
			float waterMax) {
		byte waterLavel[] = new byte[WATER_LEVEL_ALARM_DATA_LENGTH];
		waterLavel[0] = (byte) (waterMin / 256);
		waterLavel[1] = (byte) (waterMin % 256);
		waterLavel[2] = (byte) (waterMax / 256);
		waterLavel[3] = (byte) (waterMax % 256);
		byte cmd[] = new byte[SET_WATER_LEVEL_RANGE_PREFIX.length
				+ waterLavel.length];
		for (int i = 0; i < SET_WATER_LEVEL_RANGE_PREFIX.length; i++) {
			cmd[i] = SET_WATER_LEVEL_RANGE_PREFIX[i];
		}
		for (int j = 0; j < waterLavel.length; j++) {
			cmd[SET_WATER_LEVEL_RANGE_PREFIX.length + j] = waterLavel[j];
		}
		return cmd;
	}

	public static byte[] getSetPHCommand(float value) {
		byte ph[] = new byte[PH_DATA_LENGTH];
		ph[0] = (byte) ((int) value);
		ph[1] = (byte) (value * 10 % 10);
		return ph;
	}

	public static byte[] getSetTemperatureCommand(float value) {
		byte tempe[] = new byte[TEMPRATURE_DATA_LENGTH];
		tempe[0] = (byte) ((int) value);
		return tempe;
	}

	public static byte[] getSetOxygenCommand(float value) {
		byte oxygen[] = new byte[OXYGEN_DATA_LENGTH];
		oxygen[0] = (byte) (value / Math.pow(16, 2));
		oxygen[1] = (byte) (value % Math.pow(16, 2));
		return oxygen;
	}

	public static byte[] getSetConductivityCommand(float value) {
		byte conduc[] = new byte[CONDUCTIVITY_DATA_LENGTH];
		conduc[0] = (byte) ((int) value);
		conduc[1] = (byte) (value * 1000 % 1000 / Math.pow(16, 2));
		conduc[2] = (byte) (value * 1000 % 1000 % Math.pow(16, 2));
		return conduc;
	}

	public static byte[] getSetWaterLevelCommand(float value) {
		byte wl[] = new byte[WATER_LEVEL_DATA_LENGTH];
		wl[0] = (byte) (value / Math.pow(16, 2));
		wl[1] = (byte) (value % Math.pow(16, 2));
		return wl;
	}

	public static byte[] getPumpDosingCommand(int controllerId, int pumpId) {
		byte fnCode = 0;
		fnCode = (byte) (0x49 + controllerId);
		byte[] cmd = new byte[] { 0x5A, 0x01, fnCode, 0x02, (byte) pumpId, 0x00 };
		return cmd;
	}

	public static byte[] getSetAliasCommand(int[] controllerId) {
		if (controllerId.length != 6) {
			return null;
		}
		return new byte[] { 0x5A, 0x01, CODE_SET_ALIAS, 0x0c, 0x01,
				(byte) controllerId[0], 0x02, (byte) controllerId[1], 0x03,
				(byte) controllerId[2], 0x04, (byte) controllerId[3], 0x05,
				(byte) controllerId[4], 0x06, (byte) controllerId[5] };
	}
}
